package com.hengtiansoft.lfy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.been.ProductPresentationBean;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.entity.ProductEntity;
import com.hengtiansoft.lfy.entity.ServiceResult;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.chart.applib.utils.HXPreferenceUtils;

import cn.jpush.android.service.DownloadService;

/**
 * @ClassName: SplashActivity
 * @Description: 欢迎页面
 * 
 */
public class SplashActivity extends FragmentActivity {

    private static String TAG = "AboutActivity";
    private static final int sleepTime = 2000;
    private static final String SHAREDPREFERENCES_NAME = "first_pref";
    private boolean isFirstIn = false;
    private boolean hasSendJson = false;
    private boolean isShow = false;
    private String filename = null;
    private Context mContext;

    @Override
    protected void onCreate(Bundle arg0) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 取消标题栏
        super.onCreate(arg0);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 全屏
        mContext = this;
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!hasSendJson) {
            isShowRequest();
        }
        new Thread(new Runnable() {
            public void run() {
                SharedPreferences preferences = getSharedPreferences(
                        SHAREDPREFERENCES_NAME, MODE_PRIVATE);
                // 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
                isFirstIn = preferences.getBoolean("isFirstIn", true);
                        try {
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {
                        }

                if(isFirstIn) {
                    Intent intent = new Intent(SplashActivity.this,
                            GuideActivity.class);
                    SplashActivity.this.startActivity(intent);
                    SplashActivity.this.finish();
                }else{
                    // 跳转
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }).start();
    }

    /**
     * 是否显示产品介绍页
     * 
     * @param
     */
    private void isShowRequest() {
        hasSendJson = true;
        NetHelper.isShowRequest(this, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                ProductEntity info = (ProductEntity) rspData;
                int code = 1;
                String message = null;
                ProductPresentationBean data = null;
                message = info.getMessage();
                code = info.getRetcode();
                data = info.getData();
                if (code == 0) {
                    isShow = true;
                    if (data != null) {
                        filename = data.getName();
                        if (HXPreferenceUtils.getInstance()
                                .getDownloadServiceSuccess() != null
                                && !HXPreferenceUtils.getInstance()
                                .getDownloadServiceSuccess()
                                .equals(filename)) {
                            Intent intentService = new Intent(
                                    SplashActivity.this,
                                    DownloadService.class);
                            intentService.putExtra("image_path",
                                    Constants.imageURL
                                            + data.getPhotopath());
                            intentService.putExtra("filename",
                                    data.getName());
                            startService(intentService);
                        }
                    }
                } else {
                    isShow = false;
                }
                hasSendJson = false;
            }

            @Override
            public void failed(String msg) {
                isShow = false;
                hasSendJson = false;
            }
        }, ProductEntity.class);
    }
}
